package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.constant.Constant;
import com.itany.pojo.Book;
import com.itany.pojo.Examine;
import com.itany.pojo.Press;
import com.itany.pojo.Supplier;
import com.itany.service.BookService;
import com.itany.service.ExamineService;
import com.itany.service.PressService;
import com.itany.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import utils.AjaxResult;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Author：汤小洋
 * Date：2019-09-18 下午3:10
 * Description：<描述>
 */
@RequestMapping("/book")
@Controller
public class BookController {


    @Autowired
    private BookService bookService;
    @Autowired
    private ExamineService examineService;
    @Autowired
    private PressService pressService;
    @Autowired
    private SupplierService supplierService;

    @RequestMapping("/findPress")
    @ResponseBody
    public AjaxResult findAllPress(){
        List<Press> press = pressService.findAllPress();
        System.out.println(press);
        return AjaxResult.isOk(press);

    }
    @RequestMapping("/findSupplier")
    @ResponseBody
    public AjaxResult findAllSupplier(){
        List<Supplier> suppliers = supplierService.findAllSupplier();
        System.out.println(suppliers);
        return AjaxResult.isOk(suppliers);

    }

    @RequestMapping("/addBook")
    public String addBook(Book book, @RequestParam("somefile") MultipartFile somefile) throws IOException {
        Examine examine = new Examine();
        String fileName = somefile.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String newName = uuid+fileName.substring(fileName.lastIndexOf("."));
        somefile.transferTo(new File("/home/soft01/upload"+newName));
        book.setImgUrl(newName);
        book.setFlag(Constant.BOOK_EXAMINE_UNCERTAIN);
        bookService.addBook(book);

        Integer id = book.getId();
        examine.setBookId(id);
        examine.setTitle(book.getBookName()+"正在审核");
        examine.setFlag(Constant.EXAMINE_UNCERTAIN);

        examineService.addExamine(examine);

        return "supplier_examine";
    }

    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam(defaultValue="1")Integer page,
                                      @RequestParam(defaultValue="10")Integer rows,Book book){

        System.out.println(book+"11111111111111111111111");
        Map<String,Object> map = new HashMap<String, Object>();
        PageInfo<Book> info = bookService.findAllBook(page, rows,book);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    //书籍启用
    @RequestMapping("modifyStatusEnable")
    @ResponseBody
    public AjaxResult modifyStatusEnable(Book book){
        bookService.modifyStatusEnable(book);

        return AjaxResult.isOk();
    }



    //书籍禁用
    @RequestMapping("modifyStatusDisable")
    @ResponseBody
    public AjaxResult modifyStatusDisable(Book book){
        System.out.println(book);
        bookService.modifyStatusDisable(book);

        return AjaxResult.isOk();
    }


    @RequestMapping("modifyBook")
    public String modifyBook(Book book,@RequestParam("somefile") MultipartFile somefile){
        try {
        String fileName = somefile.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String newName = uuid+fileName.substring(fileName.lastIndexOf("."));
        somefile.transferTo(new File("/home/soft01/upload"+newName));
        book.setImgUrl(newName);
        bookService.modifyBook(book);



        } catch (IOException e) {
            e.printStackTrace();
        }


        return "booklist";
    }
}